package io.xccit.train.common.result;

import io.xccit.train.common.enums.ResultCode;

import java.io.Serializable;

/**
 * @author CH_ywx
 * @version 1.0
 * @create 2024/11/6
 * <p>统一返回结果</p>
 */
public class AjaxResult<T> implements Serializable {

    private Integer code;
    private String message;
    private T data;

    public AjaxResult() {}

    public AjaxResult(ResultCode resultCode,T data) {
        this.code = resultCode.SUCCESS.getCode();
        this.message = resultCode.SUCCESS.getMessage();
        if (data != null){
            this.data = data;
        }
    }

    public AjaxResult(Integer code,String message,T data){
        this.code = code;
        this.message = message;
        if (data != null){
            this.data = data;
        }
    }

    /**
     * 操作成功
     * @return
     * @param <T>
     */
    public static<T> AjaxResult<T> success(){
        return new AjaxResult<T>(ResultCode.SUCCESS,null);
    }

    /**
     * 操作成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> AjaxResult<T> success(T data){
        return new AjaxResult<T>(ResultCode.SUCCESS,data);
    }

    /**
     * 操作失败
     * @return
     * @param <T>
     */
    public static <T> AjaxResult<T> error(){
        return new AjaxResult<T>(ResultCode.ERROR,null);
    }

    /**
     * 操作失败
     * @param resultCode 结果枚举
     * @return
     * @param <T>
     */
    public static <T> AjaxResult<T> error(ResultCode resultCode){
        return new AjaxResult<T>(resultCode.getCode(),resultCode.getMessage(),null);
    }

    /**
     * 操作失败
     * @param code 状态码
     * @param message 消息
     * @return
     * @param <T>
     */
    public static <T> AjaxResult<T> error(Integer code,String message){
        return new AjaxResult<T>(code,message,null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "AjaxResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
